// This script binds all plugins, widgets and other scripts for the AGS template
$(function(){
	// Needed for website, don't edit :)
	var browser = $.browser;
	if(browser.msie && browser.version < 9)
		alert('You are using Internet Explorer ' + browser.version + '. This browser is out of date and will not display this website properly. Please upgrade to version 9.0 or switch to Mozilla Firefox or Google Chrome');
	
	var Body = $('body');
	
	Body.find('.AGS_Menu').AGS_Menu();
	Body.find('a[title]').AGS_Tooltip();
	
	// Example bindings, free to edit
	Body.find('.AGS_Form').each(function(index, Element){
		head.js("AGS_Form.js", function(){
			$(Element).AGS_Form();
		});		
	});
	
	Body.find('#sidebars').each(function(index, Element){
		loadcss("frontpage.css");
		head.js("AGS_Sidebars.js", function(){			
			$(Element).AGS_Sidebars();
		});			
	});

	Body.find('.datatable').each(function(index, Element){
		loadcss("jquery/datatables.css");
		head.js('jquery/datatable.js', function(){
			$(Element).dataTable();
		});
	});
		
	Body.find( ".progressbar" ).each(function(index, Element){
		head.js('jquery/ui/jquery.ui.progressbar.min.js', function(){
			$(Element).progressbar({ value: 37 });	
		});
	});
	
	Body.find('.wow-item-tooltip').each(function(index, Element) {
        
    });
	
	Body.find('#raidviewer').each(function(index, Element){
		var element = $(Element);
		head.js('jquery/ui/jquery.ui.sortable.min.js', function(){
			element.find('.mid li[title]').AGS_Tooltip({ show:{ delay: 1000 } });
			element.find('#signup').AGS_Dialog({ type: 'form'});
			
			element.find('.mid > div:first-child ul, .mid > div:nth-child(2) ul').sortable({
				connectWith: ".connected",
				cancel: ".disabled",
				items: "li:not(.disabled)",
				placeholder: "highlight-drop",
				update: function(event, ui) {
					if( ui.sender != null)
					{
						var player_id = ui.item.attr('data-id');
						var from = $(ui.sender).attr('id');
						var to = $(ui.item).parent().attr('id');// available, unavailable, tanks, healers, dps
						//alert('player_id(' + player_id + ')  from(' + from + ')   to css id(' + to + ')');
						$(ui.item).qtip('hide');
					}
				}
			}).disableSelection();
		});
	});
	
	Body.find('#raidcalendar').each(function(index, Element){	
		var element = $(Element);
		loadcss("jquery/calendar.css");	
			head.js('jquery/ui/jquery.ui.droppable.min.js', 'jquery/ui/jquery.ui.datepicker.min.js', 'jshashtable.js', 'jquery/calendar.js', function(){
			
				var jfcalplugin = element.jFrontierCal({
					date: new Date(),
					//dayClickCallback: myDayClickHandler,
					//agendaClickCallback: myAgendaClickHandler,
					//agendaDropCallback: myAgendaDropHandler,
					dragAndDropEnabled: true
				}).data("plugin");
				
				$("#dateSelect").datepicker({
					showOtherMonths: true,
					selectOtherMonths: true,
					changeMonth: true,
					changeYear: true,
					showButtonPanel: true,
					dateFormat: 'yy-mm-dd'
				});				
				/**
				 * Set datepicker to current date
				 */
				$("#dateSelect").datepicker('setDate', new Date());
				/**
				 * Use reference to plugin object to a specific year/month
				 */
				$("#dateSelect").bind('change', function() {
					var selectedDate = $("#dateSelect").val();
					var dtArray = selectedDate.split("-");
					var year = dtArray[0];
					// jquery datepicker months start at 1 (1=January)		
					var month = dtArray[1];
					// strip any preceeding 0's		
					month = month.replace(/^[0]+/g,"")		
					var day = dtArray[2];
					// plugin uses 0-based months so we subtrac 1
					jfcalplugin.showMonth("#" + element.attr('id'),year,parseInt(month-1).toString());
				});				
			
				$("#BtnPreviousMonth").click(function() {
					jfcalplugin.showPreviousMonth("#" + element.attr('id'));
					// update the jqeury datepicker value
					var calDate = jfcalplugin.getCurrentDate("#" + element.attr('id')); // returns Date object
					var cyear = calDate.getFullYear();
					// Date month 0-based (0=January)
					var cmonth = calDate.getMonth();
					var cday = calDate.getDate();
					// jquery datepicker month starts at 1 (1=January) so we add 1
					$("#dateSelect").datepicker("setDate",cyear+"-"+(cmonth+1)+"-"+cday);
					return false;
				});
				/**
				 * Initialize next month button
				 */
				$("#BtnNextMonth").click(function() {
					jfcalplugin.showNextMonth("#" + element.attr('id'));
					// update the jqeury datepicker value
					var calDate = jfcalplugin.getCurrentDate("#" + element.attr('id')); // returns Date object
					var cyear = calDate.getFullYear();
					// Date month 0-based (0=January)
					var cmonth = calDate.getMonth();
					var cday = calDate.getDate();
					// jquery datepicker month starts at 1 (1=January) so we add 1
					$("#dateSelect").datepicker("setDate",cyear+"-"+(cmonth+1)+"-"+cday);		
					return false;
				});				
				
				
				
			});
	});
	
	Body.find('.confirmDialog').AGS_Dialog({ type: 'confirm' });
	Body.find('.infoDialog').AGS_Dialog({ type: 'info' });
	Body.find('.formDialog').AGS_Dialog({ type: 'form' });
	
	Body.find('a[data-wow], span[data-wow]').each(function(){
		head.js('AGS_Wowtip.js', function(){
			$(Element).AGS_Wowtip();
		});
	});
	
	head.js('jquery/ui/jquery.ui.button.min.js', function(){
			$('.agsButton').button();
			$('.agsButtonIconText').each(function(){
				var user_icon = $(this).attr('rel');
				$(this).button({
					icons: {
						primary: user_icon 
					}
				});
			});
			
	});	
	$('.messageOpenener').click(function(e){
		$.AGS_Message('error', 'There was a error', 'You can not add a comment because you are not logged in');
	});

});